using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace ZSW.Framework.EditorTools
{
    /// <summary>
    /// 重新设置父物体
    /// </summary>
    public class ResetParent : EditorWindow
    {
        [SerializeField] private List<GameObject> prefabList = new List<GameObject>();
        private GameObject parent;

        [MenuItem("Tools/Transform/Reset Parent")]
        public static void Invoke()
        {
            GetWindow(typeof(ResetParent));
        }

        public void OnGUI()
        {
            SerializedObject serializedObject = new SerializedObject(this);
            SerializedProperty property = serializedObject.FindProperty("prefabList");
            EditorGUILayout.PropertyField(property, true);

            parent = (GameObject)EditorGUILayout.ObjectField("Parent", parent, typeof(GameObject), true);

            serializedObject.ApplyModifiedProperties();

            if (GUILayout.Button("Replace"))
            {
                if (prefabList.Count == 0 || parent == null)
                {
                    Debug.LogError("Prefab is null or Parent is null");
                    return;
                }
                Replace();
            }
        }
        public void Replace()
        {
            foreach (var prefab in prefabList)
            {
                if (prefab != null)
                {
                    Undo.RecordObject(prefab.transform, "Replace Parent");
                    prefab.transform.SetParent(parent.transform);
                }
            }

            prefabList.Clear();
        }
    }
}

